package top.ltc_cn.minecraft_manager.config.DataBase;

import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Objects;

@Configuration
@EnableJpaRepositories(
        basePackages = "top.ltc_cn.minecraft_manager.repository.minecraft",
        entityManagerFactoryRef = "minecraftEntityManagerFactory",
        transactionManagerRef = "minecraftTransactionManager"
)
@EntityScan(basePackages = "top.ltc_cn.minecraft_manager.entity.minecraft")
public class MinecraftDataSourceConfig {

    @Value("${spring.datasource.minecraft.url}")
    private String url;

    @Value("${spring.datasource.minecraft.username}")
    private String username;

    @Value("${spring.datasource.minecraft.password}")
    private String password;

    @Value("${spring.datasource.minecraft.max-active}")
    private int maxActive;

    @Bean(name = "minecraftDataSource")
    public DataSource minecraftDataSource() {
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setJdbcUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        dataSource.setMaximumPoolSize(maxActive);
        return dataSource;
    }

    @Bean(name = "minecraftEntityManagerFactory")
    public LocalContainerEntityManagerFactoryBean minecraftEntityManagerFactory(
            @Qualifier("minecraftDataSource") DataSource dataSource) {
        LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
        em.setDataSource(dataSource);
        em.setPackagesToScan("top.ltc_cn.minecraft_manager.entity.minecraft");

        JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        em.setJpaVendorAdapter(vendorAdapter);
        HashMap<String, Object> properties = new HashMap<>();
        properties.put("hibernate.hbm2ddl.auto", "update");
        properties.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
        em.setJpaPropertyMap(properties);

        return em;
    }

    @Autowired
    @Qualifier("minecraftEntityManagerFactory")
    @Lazy
    private LocalContainerEntityManagerFactoryBean minecraftEntityManagerFactory;

    @Bean(name = "minecraftTransactionManager")
    public PlatformTransactionManager minecraftTransactionManager() {
        return new JpaTransactionManager(Objects.requireNonNull(minecraftEntityManagerFactory.getObject()));
    }
}
